from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QMessageBox

import sys
from ui import Ui_MainWindow
from excelObject import table

from Dataloader import dataloader


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.source_path = "D://data.xls"
        self.target_folder_path = ""

        # 默认值初始化
        self.init_ui()

        # 页面切换信号与槽初始化
        self.pushButton_data_deduplication.clicked.connect(lambda :self.select_page_vice(0))
        self.pushButton_data_contrast.clicked.connect(lambda: self.select_page_vice(1))
        self.pushButton_error_detect.clicked.connect(lambda :self.select_page_vice(2))
        self.pushButton_data_shunt.clicked.connect(lambda :self.select_page_vice(3))
        self.pushButton_error_classify.clicked.connect(lambda :self.select_page_vice(4))
        self.pushButton_filtrate.clicked.connect(lambda :self.select_page_vice(5))
        self.pushButton_data_restore.clicked.connect(lambda :self.select_page_vice(6))

        self.pushButton_data_statistics.clicked.connect(lambda :self.select_page_main(1))

        # 源文件地址
        self.pushButton_select_source_path.clicked.connect(self.set_sourcefile_path)
        self.pushButton_select_target_folder.clicked.connect(self.set_outfolder_path)

        # 确认键，根据当前页面id执行不同方法
        self.pushButton_ensure.clicked.connect(lambda :self.ensure_slot(self.stackedWidget.currentIndex()))

        # page2
        self.pushButton_page2_stand_file.clicked.connect(self.set_page2_standfile_path)


    def ensure_slot(self, id):
        # 数据去重
        if id == 0:
            try:
                source_path = self.source_path
                contrast_list = self.lineEdit_page1_contrast.text().split(",")
                if contrast_list:
                    contrast_list = [int(element) for element in list(contrast_list)]
                time_col = self.lineEdit_page1_time.text()
                time_gap = self.lineEdit_page1_time_gap.text()
                out_folder_path = self.target_folder_path

                if source_path and contrast_list and time_col and time_gap and out_folder_path:
                    excelObj2 = table(source_path)
                    excelObj2.unique(cols=contrast_list, time_col=int(time_col), out_path=out_folder_path + "/", time_diff=int(time_gap))
                self.set_label_tip("数据去重完成")
            except Exception as e:
                QMessageBox.warning(self,"warning",str(e))
        # 数据对比
        elif id == 1:
            source_path = self.source_path
            standardpath = self.page2_stand_file_path
            source_cols = self.lineEdit_page2_source_file.text().split(",")
            if source_cols:
                source_cols = [int(element) for element in list(source_cols)]
            standardCols = self.lineEdit_page2_contrast.text().split(",")
            if standardCols:
                standardCols = [int(element) for element in list(standardCols)]
            out_folder_path = self.target_folder_path

            if source_path and standardpath and source_cols and out_folder_path:
                # 数据对比
                loader = dataloader(sourcepath=source_path, standardpath=standardpath)
                resdf = loader.filter(sourceCols=source_cols, standardCols=standardCols, out_path= out_folder_path + "/result.xlsx")
                self.set_label_tip("数据对比已完成")

        # 异常检测
        elif id == 2:
            # 源文件地址
            source_path = self.source_path
            # 列号
            col_list = self.lineEdit_page3_col.text().split(",")
            if col_list:
                col_list = [int(element) for element in list(col_list)]
            # 选项
            option = self.comboBox_page3.itemText(self.comboBox_page3.currentIndex())
            # 输出文件夹地址
            out_folder_path = self.target_folder_path
            if source_path and col_list and option and out_folder_path:
                '''
                    调用异常检测模块
                '''
                self.set_label_tip("异常检测已完成")
        # 数据分流
        elif id == 3:
            source_path = self.source_path
            groupingcols = self.lineEdit_page4_grouping.text().split(",")
            if groupingcols:
                groupingcols = [int(element) for element in list(groupingcols)]
            out_folder_path = self.target_folder_path
            excelObj1 = table(source_path)
            excelObj1.reGroup(groupingcols, out_path=out_folder_path + "/")
            self.set_label_tip("数据分流已完成")
        # 故障分类
        elif id == 4:
            # 源文件地址
            source_path = self.source_path
            # 故障列
            error_list = self.lineEdit_page5_error.text().split(",")
            if error_list:
                error_list = [int(element) for element in list(error_list)]
            # 分类模型
            option = self.comboBox_page5.itemText(self.comboBox_page5.currentIndex())
            # 输出地址
            out_folder_path = self.target_folder_path
            if source_path and error_list and option and out_folder_path:
                '''
                    调用故障分类模块
                '''
                self.set_label_tip("故障分类已完成")
        # 非典型筛选
        elif id == 5:
            # 源文件地址
            source_path = self.source_path
            # 故障列
            error_list = self.lineEdit_page6_error.text().split(",")
            if error_list:
                error_list = [int(element) for element in list(error_list)]
            out_folder_path = self.target_folder_path
            if source_path and error_list and out_folder_path:
                '''
                    调用非典型筛选模块
                '''
                self.set_label_tip("非典型筛选已完成")
        # 数据恢复
        elif id == 6:
            # 源文件地址
            source_path = self.source_path
            # 恢复列
            restore_list = self.lineEdit_page7_restore.text().split(",")
            if restore_list:
                restore_list = [int(element) for element in list(restore_list)]
            # 输出地址
            out_folder_path = self.target_folder_path
            if source_path and restore_list and out_folder_path:
                '''
                    调用数据恢复模块
                '''
                self.set_label_tip("数据恢复已完成")

    # 设置源文件地址
    def set_sourcefile_path(self):
        self.source_path = self.browse_file()
        self.lineEdit_source_path.setText(self.source_path)

    # 设置输出文件夹地址
    def set_outfolder_path(self):
        self.target_folder_path = self.browse_folder()
        self.lineEdit_target_folder.setText(self.target_folder_path)

    # 设置标准文件地址
    def set_page2_standfile_path(self):
        self.page2_stand_file_path = self.browse_file()
        self.lineEdit_page2_stand_file.setText(self.page2_stand_file_path)

    # 初始化combox
    def init_ui(self):
        self.lineEdit_source_path.setText(self.source_path)

        # page1初始化
        self.lineEdit_page1_contrast.setText("0,1,2,3,4,5,6")
        self.lineEdit_page1_time.setText("17")
        self.lineEdit_page1_time_gap.setText("48")
        # page2初始化
        self.lineEdit_page2_stand_file.setText(self.source_path)
        self.lineEdit_page2_source_file.setText("0,1")
        self.lineEdit_page2_contrast.setText("3,7")

        # page3初始化
        self.lineEdit_page3_col.setText("0,1")
        self.comboBox_page3.addItem("1")
        self.comboBox_page3.addItem("2")
        self.comboBox_page3.addItem("3")

        # page4初始化
        self.lineEdit_page4_grouping.setText("0,1")

        # page5初始化
        self.lineEdit_page5_error.setText("0,1")
        self.comboBox_page5.addItem("1")
        self.comboBox_page5.addItem("2")
        self.comboBox_page5.addItem("3")

        # page6初始化
        self.lineEdit_page6_error.setText("0,1")

        # page7初始化
        self.lineEdit_page7_restore.setText("0,1")

    def select_page_main(self, id):
        self.stackedWidget_main.setCurrentIndex(id)

    def select_page_vice(self, id):
        self.select_page_main(0)
        if(id==0):
            #去重
            self.set_top_tip("\n本模块负责去掉重复的记录\n在对比列中选择用来去重的列号(A为0,B为1，以此类推)\n时间列填写故障时间的列号，时差填写能够被忽略的最大时差。")
        elif(id==1):
            #对比
            self.set_top_tip("\n本模块负责根据标准表来对比筛选源文件表的数据记录\n分别在源文件列与标准列填写对比列号，注意列应当一一对应")
        elif(id==3):
            #分流
            self.set_top_tip("\n本模块负责根据指定列来进行拆分分表\n选择指定列的列号，模块将不同的数据汇聚到不同的表内，以数据行的内容组合命名")
        else:
            self.set_top_tip("\n开发中，敬请期待")
        self.stackedWidget.setCurrentIndex(id)



    def browse_file(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", "", "")
        if file_path:
            return file_path

    def browse_folder(self):
        options = QFileDialog.Options()
        options |= QFileDialog.ShowDirsOnly | QFileDialog.DontUseNativeDialog
        folder_path = QFileDialog.getExistingDirectory(self, "选择文件夹", options=options)
        if folder_path:
            return folder_path

    def set_label_tip(self, text):
        self.label_tip.setText(text)

    # 设置顶端提示
    def set_top_tip(self, text):
        self.label_top_tip.setText(text)

    # 设置数据统计页面
    def set_data_statistics_page(self, text):
        self.textEdit_data.setPlainText(text)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
